var controller = {};

controller.deleteFolder = function(req, res, next){
    var ql = 'update content set id = "deFolder" where firstnav ="' + req.query.name + '" and tel ="' + global.USER_ACCOUNT + '";';
    console.log(ql)
    conn.query(ql, function(err, results, fields){
        if(err) throw err;
        res.send({
            code: 200
        })
    })
}

controller.getFolder = function(req, res, next){
    var ql = 'select firstnav, tel, id from content where tel = "' + global.USER_ACCOUNT + '" and id != "deFolder"' + ' group by(firstnav)';
    console.log(ql);
    conn.query(ql, function(err, results, fields){
        if(err) throw err;
        var folderList =[];
        for(var i = 0; i < results.length; i++){
            var result ={
                name: results[i].firstnav,
                active: '',
                rename: '',
                id: results[i].id
            }
            folderList.push(result);
        }
        res.send({
            code: 200,
            folderList: folderList
        });
    });
}

controller.renameFolder = function(req, res, next){
    var ql = 'update content set firstnav = "' + req.query.newName + '" where firstnav="' + req.query.oldName + '" and tel ="' + global.USER_ACCOUNT + '";';
    console.log(ql)
    conn.query(ql, function(err, results, fields){
        if(err) throw err;
        res.send({
            code: 200
        })
    })
}

controller.saveFolder = function(req, res, next){
    var ql = 'insert into content(id,tel,firstnav,secondnav,time,pw,content) values ("noFile","' + global.USER_ACCOUNT + '","' + req.query.name + '","","","","");'
    console.log(ql);
    conn.query(ql, function(err, results, fields){
        if(err) throw err;
        res.send({
            code: 200
        })
    })
}

module.exports = controller;